<?php
namespace app\system\controller;

use app\common\model\PatchModel;
use think\Request;

class PatchController extends BaseController
{

    private $patchModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->patchModel = new PatchModel();
    }

    /**
     * 碎片首页
     * @return mixed
     */
    public function index()
    {
        $patchList = $this->patchModel->where('site_id', $this->getSiteId())->paginate(12, false);
        $this->assign('patchList', $patchList);
        return $this->fetch();
    }

    /**
     * 添加碎片
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data                  = $this->request->param();
            $data['patch_content'] = $this->patchModel->formatPatch($data);
            $data['site_id'] = $this->getSiteId();
            $result                = $this->validate($data, 'PatchValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $info = $this->patchModel->where('site_id',$this->getSiteId())->where('patch_sn',$data['patch_sn'])->find();
            if($info){
                $this->error('该碎片标识已存在！');
            }
            if ($this->patchModel->addData($data)) {
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->patchModel->getError());
            }
        }
        $typeList = get_dict('102');
        $this->assign('utype',$this->getUtype());
        $this->assign('typeList', $typeList);
        return $this->fetch();
    }

    /**
     * 修改碎片
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data                  = $this->request->param();
            $data['patch_content'] = $this->patchModel->formatPatch($data);
            $result                = $this->validate($data, 'PatchValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $info = $this->patchModel->where('patch_id',$data['patch_id'])->find();
            if($info['patch_sn']!=$data['patch_sn']){
                $info = $this->patchModel->where('site_id',$this->getSiteId())->where('patch_sn',$data['patch_sn'])->find();
                if($info){
                    $this->error('该碎片标识已存在！');
                }
            }
            if ($this->patchModel->editData($data)) {
                $this->success('修改成功', url('index'));
            } else {
                $this->error($this->patchModel->getError());
            }
        }
        $typeList = get_dict('102');
        $patchId  = $this->request->param('patch_id/d');
        $info     = $this->patchModel->get($patchId);
        $this->assign('utype',$this->getUtype());
        $this->assign('typeList', $typeList);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        $patchId = $this->request->param('patch_id');
        if ($this->patchModel->delData($patchId)) {
            $this->success('删除成功！');
        } else {
            $this->error($this->patchModel->getError());
        }
    }
}
